package com.hnevc.ormlitedemo.utlis;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.hnevc.ormlitedemo.entity.RechargeRecord;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;

/**
 * Created by Administrator on 2017/12/30.
 */

public class OpenHelper extends OrmLiteSqliteOpenHelper {
    //上下文
    private Context mContext;
    //创建数据库
    private OpenHelper(Context context) {
        super(context, "data.db", null, 1);
        mContext = context;
    }
    //当例模式，获取数据库访问对象
    private static OpenHelper mHelper;
    public static OpenHelper getHelper(Context context){
        if(mHelper==null){
            mHelper = new OpenHelper(context);
        }
        return mHelper;
    }

    //在数据库创建完成后，数据库中表的创建，使用TableUtils.createTable
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
        try {
            TableUtils.createTable(connectionSource, RechargeRecord.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //更新数据库
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase,
                          ConnectionSource connectionSource, int i, int i1) {
        try {
            TableUtils.dropTable(connectionSource, RechargeRecord.class, true);

        } catch (SQLException e) {
            e.printStackTrace();
        }
        onCreate(sqLiteDatabase,connectionSource);
    }




}
